import {
  Controller,
  FileTypeValidator,
  MaxFileSizeValidator,
  ParseFilePipe,
  Post,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { UploadService } from './upload.service';
import { FileInterceptor } from '@nestjs/platform-express';

@Controller('upload')
export class UploadController {
  constructor(private uploadService: UploadService) {}

  @Post('/img')
  @UseInterceptors(FileInterceptor('file'))
  async img(
    @UploadedFile(
      new ParseFilePipe({
        validators: [
          new FileTypeValidator({
            fileType: 'image/*',
          }),
          new MaxFileSizeValidator({ maxSize: 1024 * 1024 * 5 }),
        ],
      }),
    )
    file: Express.Multer.File,
  ) {
    const { originalname, buffer } = file;
    const { Location } = await this.uploadService.uploadObject(
      originalname,
      buffer,
    );
    return {
      url: Location,
    };
  }

  @Post('/model')
  @UseInterceptors(FileInterceptor('file'))
  async model(
    @UploadedFile(
      new ParseFilePipe({
        validators: [new MaxFileSizeValidator({ maxSize: 1024 * 1024 * 30 })],
      }),
    )
    file: Express.Multer.File,
  ) {
    const { originalname, buffer } = file;
    const { Location } = await this.uploadService.uploadObject(
      originalname,
      buffer,
      'model',
    );
    return {
      url: Location,
    };
  }
}
